package com.itheima.reggie.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.IDishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private IDishService dishService;

    // 菜品分页查询
    @GetMapping("/page")
    public ResultInfo findByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            String name) { // 1.接收参数

        // 2.调用service
        Page<Dish> page = dishService.findByPage(pageNum, pageSize, name);

        // 3.返回结果
        return ResultInfo.success(page);

    }

    // 新增菜品
    @PostMapping("")
    public ResultInfo save(@RequestBody Dish dish) { // 1.接收参数
        // 2.调用serivce保存
        dishService.saveDish(dish);
        // 3.返回成功结果
        return ResultInfo.success(null);
    }

    // 菜品回显
    @GetMapping("/{id}") // 前面没有#
    public ResultInfo findById(@PathVariable Long id) { // 1.接收菜品id
        // 2.调用service查询
        Dish dish = dishService.findById(id);
        // 3.返回结果
        return ResultInfo.success(dish);
    }

    // 菜品修改
    @PutMapping()
    public ResultInfo update(@RequestBody Dish dish) { // 1.接收请求体参数
        // 2.调用service修改
        dishService.updateDish(dish);
        // 3.返回成功消息
        return ResultInfo.success(null);

    }

    //删除
    @DeleteMapping()
    public ResultInfo deleteBatchIds(@RequestParam List<Long> ids){
        dishService.deleteBatchIds(ids);
        return ResultInfo.success(null);
    }

    @PostMapping("/status/{status}")
    public ResultInfo updateStatus(@PathVariable Integer status,@RequestParam List<Long> ids){
        dishService.updateStatus(status,ids);
        return ResultInfo.success(null);
    }

//    // 根据分类id查询菜品列表
//    @GetMapping("/list")
//    public ResultInfo findList(Long categoryId){
//        List<Dish> dishList = dishService.findListByCategoryId(categoryId);
//        return ResultInfo.success(dishList);
//    }

    @GetMapping("/list")
        public ResultInfo findList(Long categoryId,String name) {
            if(StrUtil.isEmpty(name)){
                List<Dish> dishList1 = dishService.findListByCategoryId(categoryId);
                return ResultInfo.success(dishList1);
            }
            List<Dish> dishList2 = dishService.getDishForName(name);
            return ResultInfo.success(dishList2);
        }
}
